/**
 * 自定义状态管理器
 * 使用方法  js 引入 vuex
 * import {mapState} from 'vuex';
 * computed: {...mapState(['seele'])},
 * 
 * 然后可以直接使用 {{seele.page_local_location_city}}
 * 调用action里面方法 this.$store.dispatch('seele/freshLocation',{});
 * 调用mutations方法 this.$store.commit('seele/setLocalInfo',amapdata);
 **/
export default {
	namespaced: true,
	state: {
		// 扫描二维码进入的首页面
		page_shop_home:{
			banner:[],
			city:'加载中...',
			city_origin:{}
		},
		
		page_near_list:[],
		page_near_list_keywords:'',
		page_near_top:[],// 置顶商家
		page_near_top_goods:[],// 推进商品
		
		page_wifi_list:[],
		//游玩景点
		page_sights_search_keywords:'',
		page_sights_location_city:'加载中',// 游玩景点 顶部位置信息
		page_sights_location_origin_data:{},// 游玩景点 顶部位置信息
		page_sights_list:[],
		page_sights_page:0,
		//本市商城
		page_local_location_city:'加载中...',//本市商城 顶部展示信息
		page_local_location_origin_data:{},//本市商城 顶部展示信息(地理信息原始数据)
		page_local_left_category:[],//本市商城 左侧分类
		page_local_left_category_index:0,//
		page_local_right_list:[],//本市商城 右侧列表
		page_local_search_keywords:'',
		
		// 视频管理
		
		// 发布视屏相关
		page_video_top_cat:[],
		page_video_top_cat_index:0,
		page_video_left_cat:[],
		page_video_left_cat_index:0,
		
		page_video_goods_list:[],
		page_video_goods_list_page:0,
		page_video_goods_choose:[],
		page_video_goods_choose_id:[],
		
		page_conf:{
			wifi_page_bg_img:'',
			act_top:[],
			buy_notice:[],
			coupon_img_1:'',
			coupon_img_2:'',
			adv_img:'',
			adv_img_2:'',
			adv_img_3:'',
			icon_vip:'',
			releas_img_1:'',
			releas_img_2:'',
			releas_img_3:'',
			wifi_page_adv_img:'',
			wifi_page_adv_img_2:'',
			wx_qr_code:'',
			mini_taopiaopiao:{}
		},
		home_show_poster_flag:0
	},
	mutations: {
		
		setLocalInfo(state,data) {
			state.page_local_location_city = data.regeocode.addressComponent.city+'•'+data.regeocode.addressComponent.district
			state.page_local_location_origin_data = data;
			// uni.setStorageSync("wanlshop:user", state);
		},
		setSightsLocalInfo(state,data){
			state.page_sights_location_city = data.regeocode.addressComponent.city+'•'+data.regeocode.addressComponent.district
			state.page_sights_location_origin_data = data;
		},
		setShopHomeInfo(state,data){
			state.page_shop_home.city = data.regeocode.addressComponent.city+'•'+data.regeocode.addressComponent.district
			state.page_shop_home.city_origin = data;
		},
		setSightsSearchKeywords(state,data){
			state.page_sights_search_keywords = data;
		},
		setLocalSearchKeywords(state,data){
			state.page_local_search_keywords = data;
		},
		resetVideoGoodsList(state,data){
			state.page_video_goods_list_page=0;
			state.page_video_goods_list = [];
			state.page_video_top_cat_index = data[0];
			state.page_video_left_cat_index = data[1];
			
		},
		clearVideoGoodsChoose(state,data){
			state.page_video_goods_choose=[];
			state.page_video_goods_choose_id=[];
		},
		setVideoGoodsChoose(state,data){
			let index = state.page_video_goods_choose_id.indexOf(data.id);
			if(index>-1){
				state.page_video_goods_choose.splice(index,1);
				state.page_video_goods_choose_id.splice(index,1);
			}else{
				state.page_video_goods_choose.push(data)
				state.page_video_goods_choose_id.push(data.id)
			}
		},
		setNearListKeywords(state,data){
			state.page_near_list_keywords = data;
		}
	},
	actions: {
		async home_top_opt({state,commit,dispatch,rootState},data){
			console.log('do home_top_opt');
			if('func' in data.item){
				switch(data.item.func){
					case "scan":
						console.log('start scan');
						uni.scanCode({
							success: function (res) {
								console.log('条码类型：' + res.scanType);
								console.log('条码内容：' + res.result);
								
								let tmp = res.result.split('#');
								if(tmp.length>1 && tmp[1].indexOf('/pages/')>-1){
									uni.navigateTo({url:tmp[1]})
								}
							},
							fail: function (err) {
								console.error('扫码失败：' + err);
							}
						});
						break;
					case 'increase_home_show_poster_flag':
						state.home_show_poster_flag = state.home_show_poster_flag+1;
						console.log(state);
						break;
					case 'turn_to_taopiapiap':
						console.log(state.page_conf.mini_taopiaopiao);
						uni.navigateToMiniProgram({
						  appId: state.page_conf.mini_taopiaopiao.appId,
						  path: state.page_conf.mini_taopiaopiao.path,
						  extraData: state.page_conf.mini_taopiaopiao.extraData,
						  success(res) {
						    // 打开成功
						  }
						})
						break;
				}
			}
			if('path' in data.item){
				let url = data.item.path;
				if(url.indexOf("shop_id=")>-1){
					url=url+data.shop_id;
				}
				console.log(url);
				uni.navigateTo({url:url})
			}
		},
		async loadShopHomeInfo({state,commit,dispatch,rootState},data){
			uni.request({
				url: '/dispatch/shop/getConfig',
				method:"POST",
				data: {shop_id:data},
				success: res => {
					console.log(res.data.banner.length)
					if(res.data.banner.length>0){
						
						state.page_shop_home.banner = res.data.banner;
					}else{
						state.page_shop_home.banner = res.data.default_banner;
					}
					console.log(state.page_shop_home);
				}
			});
		},
		async loadNearList({state,commit,dispatch,rootState},data){
			//获取地理位置
			// wx.getLocation({
			// 	success(res){
			// 		console.log(res)
			// 	}
			// })
			uni.getLocation({
				type: 'gcj02',
				success: function (res) {	
					uni.request({
						url: '/dispatch/shop/nearShopList',
						method: 'GET',
						data: {
							keywords: state.page_near_list_keywords,
							lat:res.latitude,
							lng:res.longitude
						},
						success: res => {
							state.page_near_list=res.data.list;
							state.page_near_top=res.data.top;
							state.page_near_top_goods = res.data.top_goods;
							console.log(state);
						}
					});
				}
			});
		},
		async loadWifiPageGoods({state,commit,dispatch,rootState},shop_id){
			uni.request({
				url: '/dispatch/shop/goods',
				method: 'GET',
				data: {shop_id: shop_id},
				success: res => {
					state.page_wifi_list = res.data
				}
			});
		},
		
		async initShopQrCode({state,commit,dispatch,rootState},data){
			uni.request({
				url: '/wanlshop/Shop/shopQrcode',
				// method:"POST",
				data:{
					shop_id:data
				},
				success: res => {
					console.log(res);
				}
			});
		},
		
		async loadSightsList({state,commit,dispatch,rootState},is_fresh){
			if(is_fresh){
				state.page_sights_page = 0;
				state.page_sights_list = [];
			}
			state.page_sights_page ++;
			uni.request({
				url: '/dispatch/shop/sightsList',
				method:"POST",
				data:{
					keywords:state.page_sights_search_keywords,
					page:state.page_sights_page,
				},
				success: res => {
					state.page_sights_list =state.page_sights_list.concat(res.data);
				}
			});
		},
		
		async loadGoodsCategory({state,commit,dispatch,rootState},data){
			
			uni.request({
				url: '/dispatch/shop/getGoodsCategory',
				method:"POST",
				data:{},
				success: res => {
					state.page_video_top_cat = res.data;
					state.page_video_top_cat_index = 0;
					dispatch('loadGoodsShopCategory',0);
				}
			});
		},
		async loadGoodsShopCategory({state,commit,dispatch,rootState},data){
			
			uni.request({
				url: '/dispatch/shop/getGoodsShopCategory',
				method:"POST",
				data:{},
				success: res => {
					state.page_video_left_cat = res.data;
					state.page_video_left_cat_index = 0;
					
					dispatch('loadVideoGoodsList',0);
				}
			});
		},
		async loadVideoGoodsList({state,commit,dispatch,rootState},data){
			state.page_video_goods_list_page++;
			// console.log(
			// 	state.page_video_left_cat,
			// 	state.page_video_left_cat_index,
			// 	state.page_video_top_cat,
			// 	state.page_video_top_cat_index,
			// );
			let shop_cat = state.page_video_left_cat[state.page_video_left_cat_index].id;
			let cat = state.page_video_top_cat[state.page_video_top_cat_index].id;
			uni.request({
				url: '/dispatch/shop/videoGoodsList',
				method:"POST",
				data:{
					category_id:cat,
					shop_category_id:shop_cat,
					page:state.page_video_goods_list_page
				},
				success: res => {
					state.page_video_goods_list = state.page_video_goods_list.concat(res.data);
				}
			});
		},
		
		async loadRightList({state,commit,dispatch,rootState},data){
			let cat = state.page_local_left_category[state.page_local_left_category_index];
			uni.request({
				url: '/dispatch/shop/localRightList',
				method:"POST",
				data:{category:cat.category,keywords:state.page_local_search_keywords},
				success: res => {
					state.page_local_right_list = res.data;
				}
			});
		},
		async chooseLocalLeftCate({state,commit,dispatch,rootState},data){
			state.page_local_left_category_index = data;
			dispatch('loadRightList', {})// 更新右侧列表
		},
		async loadLeftCategory({state,commit,dispatch,rootState},data){
			uni.request({
				url: '/dispatch/shop/localMiniCategory',
				success: res => {
					state.page_local_left_category = res.data;
					// 更新右侧列表
					dispatch('chooseLocalLeftCate', 0)// 更新右侧列表
					// commit('setLocalLeftCate',res.data);
				}
			});
		},
		async freshLocation({state,commit,dispatch,rootState}, data) {
			uni.getLocation({
				type: 'gcj02',
				success: function (res) {	
					var amapKey = '41ed00c06016a4c19834abf849829137'
					const amapUrl =`https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=${res.longitude},${res.latitude}&key=${amapKey}`;
					// 发起请求
					uni.request({
						url: amapUrl,
						success: (res) => {
							if (res.statusCode === 200 && res.data.status === '1') {
								if('本市商城'==data) commit('setLocalInfo',res.data);
								if('游玩景点'==data) commit('setSightsLocalInfo',res.data);
								if('商店首页'==data) commit('setShopHomeInfo',res.data);
							} else {
								console.error('获取地址失败:', res);
							}
						},
						fail(err) {
							console.error('请求失败:', err);
						}
					});
					
				}
			});
		},
		async loadPageConf({state,commit,dispatch,rootState},data){
			uni.request({
				url: '/dispatch/shop/pageConf',
				success: res => {
					state.page_conf = res.data;
				}
			});
		},
	}
};

// add /www/wwwroot/newtf.ascetic.top/application/index/controller/wanlshop/Find.php
		// 上传mp4 https://www.lichu2025.cn/index/wanlshop/find/uploadVideo
// comments_id:null
// content:"333"
// goods_ids:[]
// images:[]
// type: "video"
// video_id: 2

// uni.uploadFile({
// 	url: 'https://bucketname.aliyuncs.com',//输入你的bucketname.endpoint
// 	filePath: this.$refs.uUpload.lists[0].url,//文件路径可以是临时路径 blob
// 	fileType: 'image',
// 	name: 'file',
// 	formData: {
// 		name: nameStr,
// 		key: nameStr,
// 		policy: Oss.policy, // policy
// 		OSSAccessKeyId: Oss.OSSAccessKeyId, // 输入你的AccessKeyId
// 		success_action_status: '200',
// 		signature: Oss.signature // signature
// 	},
// 	success: res => {
// 		console.log(res)
// 	}
// })